<?php include (dirname(__FILE__)."/../include/header.php"); ?>
<?php if(!isset($_SESSION['_shop'])){header("location:../user_login.php");}?>
<?php  
function upload_image($imgpath)
     {
       $name=$imgpath["name"];
       $tmp_name=$imgpath["tmp_name"];
	   $filetype = pathinfo($name, PATHINFO_EXTENSION);//获取后缀
       $size=$imgpath["size"];
	   $filename=date("YmdHis",time()).rand(1000,9999).".".$filetype;
	   $filepath="/userfiles/foodlogo/".$filename;
       $uploadfile = "../userfiles/foodlogo/".$filename;
       if(move_uploaded_file($tmp_name,$uploadfile))
         return $filepath; 
     }
	 
 if($_FILES['image']['size']>0){
     $image=$_FILES['image'];
      $image=upload_image($image);
 }else{
    $image=$_REQUEST['xiaotu'];
 }
?>
<?php
	$food_id = $_REQUEST['id'];
	$food['cat_id']=$_REQUEST['cat_id'];
	if($food_id>0){
	$sql="select * from zy_food where food_id =".$food_id;
	//echo $sql;
	$result=mysql_query($sql);
	$zy_food=mysql_fetch_array($result);
	}
	if(!empty($_POST['Submit'])){
		$food_name=$_POST['food_name'];
		$cat_id=$_POST['cat_id'];
	//	$image=$_POST['image'];
		$price=$_POST['price'];
		$notSale=$_POST['notSale'];
		$sort=$_POST['sort'];
		if($food_name!="" && $price!=""){
		if($food_id>0){
            $sql2 = "update zy_food set food_name= '".$food_name."',image='".$image."',sort='".$sort."',price='".$price."',cat_id='".$cat_id."',notSale='".$notSale."' where food_id='".$food_id."'";
		}else{
			$sql2="insert into zy_food(cat_id,shop_id,food_name,image,price,notSale,remark,is_delete,create_user,create_time,update_user,update_time,sort) values('$cat_id','".$_SESSION['_shop'][shop_id]."','$food_name','$image','$price','$notSale','qq',0,'ww',now(),'ww',now(),'$sort')";
		}
		mysql_query($sql2);
		if($food_id>0){
			header("location:food_list.php");
		}else{
			header("location:food_edit.php?cat_id=".$food['cat_id']);
		}
		}else{
			$error="请填写正确食品信息";
		}
	}
?>
<?php
	if($food_id>0){
		$sql1 = "select * from zy_food where food_id= '".$food_id."'";
		$result1 = mysql_query($sql1);
		$food = mysql_fetch_array($result1);
	}
?>
  <div class="container">
    <ul class="breadcrumb">
      <li> 店铺名称 <span class="divider">/</span> </li>
      <li><a href="<?php echo SITE_ROOT;?>/shop/edit_password.php"">修改密码</a></li>
    </ul>
    <div class="row">
      <div class="span12 wash-paper user-center profile">
        <div class="row">
          <div class="span3">
           <?php include (dirname(__FILE__)."/../include/shop/left.php"); ?>
          <div class="span9">
            <div class="main-content">
              <div class="content-header">
                <h3>添加餐品</h3>
              </div>
              <div class="content-inner changepwd">
                <form class="form-horizontal" method="post" id="changepwd_form" action="" enctype="multipart/form-data">
				<div class="field_line" ><font color="red"><?php echo $error;?></font></div>
                  <fieldset>
                    <div class="control-group clear-fix">
                      <label class="control-label" for=""><span class="required">*</span>餐品名称</label>
                      <div class="controls">
                        <input name="food_name" type="text" id="sf_guard_user_old_pwd" <?php if($food_id>0){ ?> value="<?php echo $food['food_name'];?>"<?php } ?>/>
                        <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                    <?php if($zy_food['image']!=""){?>
                     <div class="control-group clear-fix">
                      <label class="control-label" for=""><span class="required">*</span>样品图片</label>
                      <div class="controls">
                      <img src="<?php echo SITE_ROOT ?>/<?php echo $zy_food['image'];?>" style="width:50px;height:50px;" />
                       <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                        <?php } ?>
                    <!--end clearfix-->
                    <div class="control-group clear-fix">
                      <label class="control-label" for="">添加/修改图片</label>
                      <div class="controls">
                        <input type="hidden" name="xiaotu" value="<?php echo $zy_food['image'];?>" />
                        <input name="image" type="file" id="image" />
                        <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                    <div class="control-group clear-fix">
                      <label class="control-label" for=""><span class="required">*</span>所属分类</label>
                      <div class="controls">
                        <select name="cat_id" id="cat_id">
							 <?php
								$sql = "select * from zy_category where shop_id='".$_SESSION['_shop']['shop_id']."'";
								$result = mysql_query($sql);
								while($cat = mysql_fetch_array($result)){
								
							?>
								<option value="<?php echo $cat['cat_id'];?>"  <?php if($cat['cat_id'] == $food['cat_id']){echo "selected=\"selected\"";}?> ><?php echo $cat['cat_name'];?></option>
						<?php
								}
						 ?>
						</select>	  
                        <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                    <!--end clearfix-->
					<div class="control-group clear-fix">
                      <label class="control-label" for=""><span class="required">*</span>菜品分类</label>
                      <div class="controls">
                        <input name="sort" type="text" id="sort" <?php if($food_id>0){ ?> value="<?php echo $food['sort'];?>"<?php } ?>/>
                        <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                    <div class="control-group clear-fix">
                      <label class="control-label" for=""><span class="required">*</span>单价</label>
                      <div class="controls">
                        <input name="price" type="text" id="sf_guard_user_old_pwd" <?php if($food_id>0){ ?> value="<?php echo $food['price'];?>"<?php } ?>/>
                        <p class="help-block"></p>
                      </div>
                      <!--end input--> 
                    </div>
                    <!--end clearfix-->
                    <div class="control-group clear-fix">
                     <label class="control-label" for="">供应状态</label>
                     <div class="controls">
                      <select name="notSale" id="notSale">
							  <option value="0" <?php if($food['notSale'] == 0){ echo "selected='selected'";}?>>正在销售</option>
							  <option value="1" <?php if($food['notSale'] == 1){ echo "selected='selected'";}?>>暂停供应</option>
					  </select>
					</div>
				    </div>
                    <div class="form-actions">
                      <input type="submit" class="btn btn-yellow" name="Submit" id="sf_guard_user_changepwd_submit" value="提交"></input>
                    </div>
                  </fieldset>
                </form>
              </div>
              <!--end contentarea--> 
            </div>
            <!--end marin-content--> 
          </div>
        </div>
      </div>
    </div>
  </div>
  <a id="back-to-top" href="javascript:void(0);">
  <div class="backtotop"></div>
  </a> </div>
<?php include (dirname(__FILE__)."/../include/footer.php"); ?>
